// Scripts/Utils/TilePool.cs
using System.Collections.Generic;
using UnityEngine;

public class TilePool : MonoBehaviour
{
    public GameObject tilePrefab;
    public int poolSize = 64;
    
    private Queue<GameObject> tilePool = new Queue<GameObject>();
    
    void Start()
    {
        InitializePool();
    }
    
    void InitializePool()
    {
        for (int i = 0; i < poolSize; i++)
        {
            GameObject tile = Instantiate(tilePrefab, transform);
            tile.SetActive(false);
            tilePool.Enqueue(tile);
        }
    }
    
    public GameObject GetTile()
    {
        if (tilePool.Count > 0)
        {
            GameObject tile = tilePool.Dequeue();
            tile.SetActive(true);
            return tile;
        }
        else
        {
            // 如果池子空了，创建新的
            return Instantiate(tilePrefab, transform);
        }
    }
    
    public void ReturnTile(GameObject tile)
    {
        tile.SetActive(false);
        tilePool.Enqueue(tile);
    }
}